package com.github;

import com.github.bean.User;
import com.github.common.constant.MyConstant;
import com.github.manager.Manager;
import com.github.service.UserServer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class DataController {
    @Autowired
    private UserServer userServer;

    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("/api/get_account")
    public String getAccount(@RequestParam("token") String token) {
        return (String) stringRedisTemplate.opsForHash().get(MyConstant.UserLoginToken,token);
    }

    @GetMapping("/api/del_token_by_account")
    public void delTokenByAccount(String account) {
        stringRedisTemplate.opsForHash().delete(MyConstant.UserLoginAccount,account);
    }

    @GetMapping("/api/del_account_by_token")
    public void delAccountByToken(String token) {
        stringRedisTemplate.opsForHash().delete(MyConstant.UserLoginToken,token);
    }

    @GetMapping("/api/get_account_by_mysql")
    public String getAccountByMysql(String account) {
        User user = userServer.getUser(account);
        if (user == null) {
            return "";
        }
        return user.getAccount();
    }
}